import random
import json
import argparse

# 定义平台列表
PLATFORMS = [
    "京东", "拼多多", "抖音商城", "天猫国际", "药房网商城", 
    "淘宝", "携程", "唯品会", "考拉海购", "妈妈网孕育"
]

# 定义评论类型
COMMENT_TYPES = ["积极", "消极", "中立"]

# 定义各类别的价格区间
PRICE_RANGES = {
    "蛋白粉": [(0, 20), (21, 50), (51, 80), (81, 100), (101, 150), (151, 200), (201, 250), (251, 300), (301, 500), (501, 1000)],
    "纸巾": [(0, 10), (11, 20), (21, 30), (31, 40), (41, 50), (51, 100), (101, 150), (151, 200), (201, 300), (301, 600)],
    "胶囊": [(0, 20), (21, 50), (51, 80), (81, 100), (101, 150), (151, 200), (201, 300), (301, 400), (401, 500), (501, 1000)],
    "弓箭": [(100, 150), (151, 200), (201, 250), (251, 300), (301, 350), (351, 400), (401, 500), (501, 600), (601, 800), (801, 1500)],
    "电脑": [(1000, 1500), (1501, 2000), (2001, 2500), (2501, 3000), (3001, 3500), (3501, 4000), (4001, 5000), (5001, 6000), (6001, 8000), (8001, 15000)],
    "膏药贴": [(0, 20), (21, 40), (41, 60), (61, 80), (81, 100), (101, 150), (151, 200), (201, 250), (251, 300), (301, 600)],
    "医疗器械": [(50, 100), (101, 150), (151, 200), (201, 300), (301, 400), (401, 500), (501, 600), (601, 800), (801, 1000), (1001, 2000)],
    "包": [(50, 100), (101, 150), (151, 200), (201, 250), (251, 300), (301, 400), (401, 500), (501, 600), (601, 800), (801, 1500)],
    "衣服": [(50, 100), (101, 150), (151, 200), (201, 250), (251, 300), (301, 400), (401, 500), (501, 600), (601, 800), (801, 1500)],
    "手表": [(100, 300), (301, 500), (501, 800), (801, 1000), (1001, 1500), (1501, 2000), (2001, 3000), (3001, 4000), (4001, 5000), (5001, 10000)]
}

# 默认价格区间（当商品类别不在预定义列表中时使用）
DEFAULT_PRICE_RANGE = [(0, 50), (51, 100), (101, 200), (201, 300), (301, 400), (401, 500), (501, 600), (601, 800), (801, 1000), (1001, 2000)]

def get_random_price(label):
    """根据商品类别获取合适的随机价格"""
    # 获取对应类别的价格区间，如果不存在则使用默认区间
    ranges = PRICE_RANGES.get(label, DEFAULT_PRICE_RANGE)
    
    # 随机选择一个价格区间
    price_range = random.choice(ranges)
    
    # 在选定的区间内随机生成价格
    return random.randint(price_range[0], price_range[1])

def add_random_fields(data):
    """
    为每个值添加随机字段
    """
    if isinstance(data, dict):
        # 如果是字典，不要再包装一层，而是直接添加随机字段
        result = data.copy()  # 复制原始数据
        
        # 获取商品类别并生成合适的价格
        label = result.get("label", "")
        result["price"] = get_random_price(label)
        
        result["platform"] = random.choice(PLATFORMS)
        result["comment"] = random.choice(COMMENT_TYPES)
        return result
    elif isinstance(data, list):
        # 如果是列表，为每个元素添加随机字段
        return [add_random_fields(item) for item in data]
    else:
        # 如果是简单值，则包装成字典
        label = ""  # 简单值没有标签信息，使用默认价格区间
        return {
            "value": data,
            "price": get_random_price(label),
            "platform": random.choice(PLATFORMS),
            "comment": random.choice(COMMENT_TYPES)
        }

def main():
    # 设置命令行参数
    parser = argparse.ArgumentParser(description='为JSON数据添加随机字段')
    parser.add_argument('-i', '--input', required=True, help='输入JSON文件路径')
    parser.add_argument('-o', '--output', required=True, help='输出JSON文件路径')
    
    args = parser.parse_args()
    
    # 读取输入文件
    with open(args.input, 'r', encoding='utf-8') as f:
        input_data = json.load(f)
    
    # 处理数据
    result = {}
    for key, value in input_data.items():
        result[key] = add_random_fields(value)
    
    # 写入输出文件
    with open(args.output, 'w', encoding='utf-8') as f:
        json.dump(result, f, ensure_ascii=False, indent=2)
        
    print(f"处理完成！结果已保存到: {args.output}")

if __name__ == "__main__":
    main() 